package com.cityfood.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.pojo.SimpleProduct;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfUserCollect;
import com.cityfood.pojo.CfUserRecord;
import com.cityfood.service.ProductService;
import com.cityfood.service.UserRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class UserRecordController {

	@Autowired
	private UserRecordService recordService;
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value="/record",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getRecordByUserId(@RequestParam Integer userId,@RequestParam(defaultValue="1") Integer currentPage){
		CityFoodResult cityFoodResult = new CityFoodResult();
		//5.获取分页信息
		PageInfo<CfUserRecord> recordListPage = recordService.getRecordByUserIdAndPage(userId, currentPage);	
		
		if (recordListPage.getList().size()>0) {
			List<SimpleProduct> simpleProductList = new ArrayList<SimpleProduct>();
			int i = 0;
			for (CfUserRecord ur : recordListPage.getList()) {
				if (i>10) {
					break;
				}
				CfProduct product = productService.getProductByProductId(ur.getProductId());
				SimpleProduct simpleProduct = new SimpleProduct();
				simpleProduct.setFoodId(product.getId());
				simpleProduct.setFoodName(product.getName());
				simpleProduct.setFoodTitle(product.getSubTitle());
				simpleProduct.setPic(product.getMainImage());
				simpleProductList.add(simpleProduct);
				i++;
			}

			Map<String,Object> ms = new LinkedHashMap<>();
			ms.put("total", recordListPage.getTotal());
			ms.put("records", simpleProductList);
			
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("查询用户的浏览记录成功");
			cityFoodResult.setData(ms);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("您还没有留下任何足迹");
			cityFoodResult.setData("{}");
		}
		return cityFoodResult;
	}
	
	@RequestMapping(value="/record",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertRecordByUserId(@RequestParam Integer userId,@RequestParam Integer productId){
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		CfUserRecord userRecord = recordService.getRecordByUserIdAndProductId(userId, productId);
		
		if (userRecord!=null) {
			userRecord.setCreateTime(new Date());
			recordService.updateRecord(userRecord);
		}
		
		CfUserRecord record = new CfUserRecord();
		record.setUserId(userId);
		record.setProductId(productId);
		record.setCreateTime(new Date());
		int i  = recordService.insertRecord(record);
		
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("添加浏览记录成功");
			cityFoodResult.setData(true);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("添加浏览记录失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
}
